// [[ OnflowGenerator File UUID: 81fe081ebfb94525b5b42e1beadf47b3  ]]
package com.itdct.onflow.system.log.service

import com.itdct.onflow.core.constant.CommonString
import com.itdct.onflow.core.constant.ReturnCode
import com.itdct.onflow.core.extend.*
import com.itdct.onflow.core.util.KBeanCopyUtil
import com.itdct.onflow.core.vo.RespPageVo
import com.itdct.onflow.core.vo.RespVo
import com.itdct.onflow.system.common.base.BaseSysService
import com.itdct.onflow.system.common.context.Context
import com.itdct.onflow.system.common.extend.dict
import com.itdct.onflow.system.file.manager.SysTransferCenterManager
import com.itdct.onflow.system.log.entity.SysApiLogEntity
import com.itdct.onflow.system.log.mapper.SysApiLogMapper
import com.itdct.onflow.system.log.qo.SysApiLogEditQo
import com.itdct.onflow.system.log.qo.SysApiLogSearchQo
import com.itdct.onflow.system.log.vo.SysApiLogDetailVo
import com.itdct.onflow.system.log.vo.SysApiLogExportVo
import com.itdct.onflow.system.log.vo.SysApiLogListVo
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * @author DCTANT
 * @date 2025-09-23 10:57:51
 * @version 1.0
 * @description 系统接口访问日志服务
 */
@Service
class SysApiLogService(
    val sysApiLogMapper: SysApiLogMapper,
    val sysTransferCenterManager: SysTransferCenterManager
) : BaseSysService() {

    @Transactional(rollbackFor = [Exception::class])
    fun add(qo: SysApiLogEditQo, context: Context): RespVo<Long> {
        val addEntity = qo.toEntity(SysApiLogEntity::class.java)
        val id = sysApiLogMapper.insertEx(addEntity)
        return success(id)
    }

    @Transactional(rollbackFor = [Exception::class])
    fun update(qo: SysApiLogEditQo, context: Context): RespVo<Unit> {
        val updateEntity = sysApiLogMapper.selectByIdEx(qo.id)
        qo.injectValue(updateEntity)
        sysApiLogMapper.updateEx(updateEntity)
        return success()
    }

    fun delete(qo: SysApiLogEditQo, context: Context): RespVo<Unit> {
        val ids = qo.ids ?: fail(ReturnCode.LACK_PARAM, CommonString.REQUIRE_DELETE_ID_LIST)
        val deleteEntities = sysApiLogMapper.selectByIdsEx(ids)
        sysApiLogMapper.deleteBatchEx(deleteEntities)
        return success()
    }

    fun detail(qo: SysApiLogSearchQo, context: Context): RespVo<SysApiLogDetailVo> {
        val id = qo.id ?: fail(ReturnCode.LACK_PARAM, CommonString.REQUIRE_ID)
        val vo = sysApiLogMapper.findById(id) ?: fail(CommonString.ENTITY_NOT_FOUND_USAGE("系统接口访问日志", id))
        return success(vo.dict())
    }

    fun page(qo: SysApiLogSearchQo, context: Context): RespPageVo<SysApiLogListVo> {
        startPage(context)
        val pageData = sysApiLogMapper.findByList(qo)
        return successPage(pageData.dict())
    }

    fun export(qo: SysApiLogSearchQo, context: Context): RespVo<Unit> {
        sysTransferCenterManager.asyncExport(context, "system", "系统接口访问日志", 500L, {
            sysApiLogMapper.findByList(qo)
        }, { listVo ->
            val exportVo = KBeanCopyUtil.copyNewInstance(listVo, SysApiLogExportVo::class.java)
            exportVo
        })
        return success()
    }

// [[ OnflowGenerator Range (- selectFunctions -) Start ]]

// [[ OnflowGenerator Range (- selectFunctions -) End ]]
}